package com.qf.controller;

import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * @author 千锋教育
 * @Company http://www.mobiletrain.org/
 * @Version 1.0
 */
@Controller
public class DownloadController {

    @RequestMapping("/download")
    public void download(String imgName, HttpServletResponse response, HttpSession session) throws IOException {

        String realPath = session.getServletContext().getRealPath("/upload");
        if(!new File(realPath).exists()){
            new File(realPath).mkdirs();
        }

        //设置响应头  告知浏览器，要以附件的形式保存内容   filename=浏览器显示的下载文件名
        response.setHeader("content-disposition","attachment;filename="+imgName);

        IOUtils.copy( new FileInputStream(realPath + "/" + imgName), response.getOutputStream());
    }
}
